<?php

namespace backend\controllers;

use Yii;
use common\jobs\LoadCouponDataJob;
use common\models\CouponCategory;
use backend\models\CouponCategorySearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;

/**
 * CouponCategoryController implements the CRUD actions for CouponCategory model.
 */
class CouponCategoryController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ]
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all CouponCategory models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new CouponCategorySearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single CouponCategory model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new CouponCategory model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new CouponCategory();
        $model->created_at=time();
        $model->updated_at=time();
        if(Yii::$app->request->isPost){

            $model->pic = UploadedFile::getInstance($model,'pic');
            if($model->pic){
                $data=$model->upload();
                if($data){

                    $model->image=$data;
                }else{
                    return json_encode($model->errors);
                }
            }


        }
        if ($model->load(Yii::$app->request->post())  && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing CouponCategory model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $model->updated_at=time();
        if(Yii::$app->request->isPost){

            $model->pic = UploadedFile::getInstance($model,'pic');
            if($model->pic){
                $data=$model->upload();
                if($data){

                    $model->image=$data;
                }else{
                    return json_encode($model->errors);
                }
            }


        }
        if ($model->load(Yii::$app->request->post())   && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing CouponCategory model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the CouponCategory model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return CouponCategory the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = CouponCategory::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
    public function actionLoadData()
    {

        $couponCategoryList = CouponCategory::find()->all();

        foreach($couponCategoryList as $couponCategory){

            $mark_id=$couponCategory['mark_id'];
//            echo $mark_id."拉取中<br>";
            Yii::$app->queue_load_coupon_data->push(new LoadCouponDataJob([
                'couponCategoryMarkId' => $mark_id
            ]));


        }

        return $this->redirect(['index']);


    }
    public function actionSync(){
        $database = Yii::$app->mongo->getDatabase();
        $coupon_collection = $database->getCollection("coupon_category")->find();
        foreach($coupon_collection as $item){
            $model = CouponCategory::findOne(['id'=>$item['id']]);
            if(!empty($model)){

                $model->status=$item['status'];
                $model->save();
            }

        }
        return $this->redirect(['index']);
    }
}
